/*
 * FolderGridAdapter.java
 * classes : com.ruint.lib.mediachooser.adapter.FolderGridAdapter
 * @author ruint
 * V 1.0.0
 * Create at 2014-12-4 下午3:26:33
 */
package com.ruint.lib.mediachooser.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruint.core.adapter.BaseListAdapter;
import com.ruint.core.utils.ViewHolder;
import com.ruint.core.utils.bitmap.ImageLoaderUtils;
import com.ruint.corelib.R;
import com.ruint.lib.mediachooser.core.BucketEntry;
import com.ruint.lib.mediachooser.core.MediaChooserConstants;
import com.ruint.lib.mediachooser.fragment.BucketVideoFragment;

/**
 * com.ruint.lib.mediachooser.adapter.FolderGridAdapter
 * 
 * @author ruint <br/>
 *         create at 2014-12-4 下午3:26:33
 */
public class BucketGridAdapter extends BaseListAdapter<BucketEntry> {

  public BucketVideoFragment mVideoFragment;

  private int mWidth;

  public BucketGridAdapter(Context context) {
    super(context);
    mWidth = context.getResources().getDisplayMetrics().widthPixels;
  }

  public void addLatestEntry(String url) {
    int count = mList.size();
    boolean success = false;
    for (int i = 0; i < count; i++) {
      if (mList.get(i).getBucketName().equals(MediaChooserConstants.folderName)) {
        mList.get(i).setBucketUrl(url);
        success = true;
        break;
      }
    }
    if (!success) {
      BucketEntry latestBucketEntry = new BucketEntry(0, MediaChooserConstants.folderName, url);
      mList.add(0, latestBucketEntry);
    }
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.view_grid_bucket_item, parent, false);
    }
    ImageView thumbnail = ViewHolder.get(convertView, R.id.bucket_item_image);
    TextView info = ViewHolder.get(convertView, R.id.bucket_item_title);
    FrameLayout.LayoutParams imageParams = (LayoutParams) thumbnail.getLayoutParams();
    imageParams.width = mWidth / 2;
    imageParams.height = mWidth / 2;
    thumbnail.setLayoutParams(imageParams);
    if (mList != null && mList.size() > 0) {
      ImageLoaderUtils.getInstance().displayImage(mList.get(position).getBucketUrl(), thumbnail);
      info.setText(mList.get(position).getBucketName());
    }
    return convertView;
  }
}
